package com.imooc.service.impl;

import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.service.UserService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper userMapper;
@Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUsernameIsExist(String username) {
        Users user =new Users();
        user.setUsername(username);
        Users result = userMapper.selectOne(user);

        return result != null ? true : false;
    }
@Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String pwd) {

        Example userExample=new Example(Users.class);
       Example.Criteria criteria = userExample.createCriteria();
       criteria.andEqualTo("username",username);
        criteria.andEqualTo("password",pwd);

        Users result = userMapper.selectOneByExample(userExample);


        return result;
    }

    @Override
    public Users saveUser(Users user) {
        String userId=sid.nextShort();
//TODO 为每个用户生成一个二维码
        user.setQrcode("");

        user.setId(userId);
        userMapper.insert(user);
        return user;
    }
}
